package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	//"fmt"
)

type AccountController struct {
	beego.Controller
}

func (this *AccountController) Get() {
	this.Layout="layout/accountlayout.html"
	isExit := this.GetString("isExit") == "true"
	if isExit {
		this.Ctx.SetCookie("email","",0,"/")
		this.Ctx.SetCookie("password","",0,"/")
		this.Redirect("/home",301)
	}
	if !checkAccount(this.Ctx) {
		this.Redirect("/home",301)
	}
	this.Data["IsLogin"] = checkAccount(this.Ctx)
	this.TplNames = "account/index.html"
}


func (this *AccountController) Post() {
	Email := "a@a.a"
	Password := "success"
	em 	:= this.GetString("email")
	pwd 	:= this.GetString("password")
	autologin 	:= this.GetString("autologin") == "on"

	this.Data["email"] = em
	this.Data["password"] = pwd
	this.Data["autologin"] = autologin

	if 	Email == em && Password == pwd {
		maxExpre := 0
		if autologin ==true{
			maxExpre = 1<<31 - 1
		}
		this.Ctx.SetCookie("email",em,maxExpre,"/")
		this.Ctx.SetCookie("password",pwd,maxExpre,"/")
		this.Redirect("/account",301)
	}
	this.Redirect("/home/login",301)
	return
}


func checkAccount(ct *context.Context) bool{
	Email := "a@a.a"
	Password := "success"

	ck,err := ct.Request.Cookie("email")
	if err != nil{
		return false
	}
	em := ck.Value

	ck,err = ct.Request.Cookie("password")
	if err != nil{
		return false
	}
	pwd := ck.Value

	return em == Email && pwd == Password
}







func (this *AccountController) Editavatar() {
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplNames = "account/profile_edit_avatar.html"
}



func (this *AccountController) UploadChangeHead() {
	this.Data["json"] = map[string]string{"sheight": "200", "state": "2", "swidth": "180", "url": "http://www.zhaopins.com/images/temp/20150624_head_1435157748163_3205.jpg?1435157748164"}
	// this.TplNames = "account/uploadChangeHead.html"
	this.ServeJson()
}


func (this *AccountController) uploadTrueHead() {
	this.Data["json"] = map[string]string{"sheight": "200", "state": "2", "swidth": "180", "url": "http://www.zhaopins.com/images/temp/20150624_head_1435157748163_3205.jpg?1435157748164"}
	this.ServeJson()
}





